﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Samples.ServiceHosting.StorageClient;

namespace BackupObjects.Taging
{
    /// <summary>
    /// Tag row entity. Used to persist Tab object in the cloud.
    /// Used to tag an object from repository.
    /// </summary>
    public class TagRow : TableStorageEntity,IIdentifiableEntity
    {
        public TagRow()
        {
            PartitionKey = PartitionName;
        }

        /// <summary>
        /// The name of partitionName;
        /// </summary>
        private const string PartitionName="Tag";

        /// <summary>
        /// Gets or sets the id of tag.
        /// </summary>
        public Guid Id { get; set; }

        /// <summary>
        /// Gets or sets the name of the tag.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the id of parent tag.
        /// </summary>
        public string Parent { get; set; }

        public override sealed string PartitionKey
        {
            get
            {
              return base.PartitionKey;
            }
            set
            {
                base.PartitionKey = value;
            }            
        }

        public override string RowKey
        {
            get { return Id.ToString(); }
            set
            {
                Id = Guid.Parse(value);
            }            
        }

        public static implicit operator Tag(TagRow tagRow)
        {
            if(tagRow==null)
            {
                return null;
            }
            Tag tag= new Tag()
                       {
                           Id = tagRow.Id,
                           Name = tagRow.Name,                    
                       };

            if(!string.IsNullOrWhiteSpace(tagRow.Parent))
            {
                tag.Parent = Guid.Parse(tagRow.Parent);
            }

            return tag;
        }

        public static implicit operator TagRow(Tag tag)
        {
            if(tag==null)
            {
                return null;
            }
            return  new TagRow()
                       {
                           Id = tag.Id,
                           Name = tag.Name,                          
                           Parent = tag.Parent.HasValue ? tag.Parent.ToString() : string.Empty
                       };
        }

    }
}
